package com.chixing.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 连接数据库与关闭数据库连接
 */
public class DBConn {

    private static  Connection connection = null;
    //获得数据库连接
    public static Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        String url ="jdbc:mysql://localhost:3306/empdb?serverTimezone=GMT&characterEncoding=utf8";
        String username = "root";
        String password = "root";

        try {
            connection = DriverManager.getConnection(url,username,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return connection;
    }

    // 关闭数据库
    public static void closeConnection(){
        if(connection !=null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}
